package com.cdtu.controller;

import com.cdtu.entity.Daily;
import com.cdtu.entity.PersonalPlan;
import com.cdtu.entity.SysEmployee;
import com.cdtu.service.DailyService;
import com.cdtu.service.PersonalPlanService;
import com.cdtu.service.SysEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/daily")
//解决跨域问题
@CrossOrigin
public class DailyController extends BaseController{
    @Autowired
    private DailyService dailyService;
    @Autowired
    private SysEmployeeService sysEmployeeService;

    @RequestMapping("/list")
    public ModelAndView dailyList(ModelAndView modelAndView) {

        List<Daily> list = dailyService.list();
        List<SysEmployee> sysEmployeeList = sysEmployeeService.list();
        //返回数据
        modelAndView.addObject("dailyList", list);
        modelAndView.addObject("sysEmployeeList", sysEmployeeList);
        //2.跳转页面;
        modelAndView.setViewName("train/daily/DailyList");
        return modelAndView;
    }

    @RequestMapping("/toAddUI")
    public ModelAndView toAddUI(ModelAndView modelAndView) {
        //2.跳转页面;
        modelAndView.setViewName("train/daily/addDaily");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/add")
    public ModelAndView addDaily(Daily daily,ModelAndView modelAndView) {
        daily.setCreactTime(new Date());
        dailyService.save(daily);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/daily/list");
        return modelAndView;
    }

    @RequestMapping("/toUpdateUI/{dailyId}")
    public ModelAndView toUpdateUi(ModelAndView modelAndView,@PathVariable("dailyId") Long dailyId) {
        Daily daily = dailyService.getById(dailyId);
        SysEmployee sysEmployee = sysEmployeeService.getById(daily.getEmpId());
        //返回数据
        modelAndView.addObject("daily", daily);
        modelAndView.addObject("sysEmployeeName", sysEmployee.getName());
        //跳转页面;
        modelAndView.setViewName("train/daily/updateDaily");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/update")
    public ModelAndView update(ModelAndView modelAndView,Daily daily) {
        dailyService.updateById(daily);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/daily/list");
        return modelAndView;
    }

    @Transactional
    @RequestMapping("/delete/{dailyId}")
    public ModelAndView delete(@PathVariable("dailyId") Long dailyId, ModelAndView modelAndView) {

        boolean remove = dailyService.removeById(dailyId);
        modelAndView.setViewName("redirect:/daily/list");
        return modelAndView;
    }
}
